import {
    LoginWay
} from "@/api/login";
import {
    SetToken,
    GetToken
} from "@/utils";


const state =  {  
        token: GetToken('Token'),
        username: GetToken('UserName'),
        beacklist:[]
}
const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
        SetToken('Token', token)
    },
    SET_USERNAME: (state, username) => {
        state.username = username
        SetToken('UserName', username)
    },
    REM_TOKEN: (state) => {
        localStorage.clear()
        state.token = ''
        state.username = ''
    },
    SET_BREAD:(state,data)=>{
        state.beacklist = data
    },
    
}
const actions = {
    async login({
        commit,
    }, userInfo) {
        let res = await LoginWay(userInfo)
        try {
            if (res.meta.status == 200) {
                commit('SET_TOKEN', res.data.token)
                commit('SET_USERNAME', res.data.username)
                return Promise.resolve(res)
            } else {
                return Promise.resolce()
            }

        } catch {
            return Promise.reject('err2------')
        }
    },
    loginout({commit}){
        commit('REM_TOKEN')
    }

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}